import Vue from 'vue'
import Toast from './Toast.vue'

// 创建组件构造器
// ToastConstructor是Toast组件的构造函数，可以通过New进行实例化
const ToastConstructor = Vue.extend(Toast)
console.log('ToastConstructor', ToastConstructor)

function showToast (text, duration = 2000) {
    const toastDOM = new ToastConstructor({
        el: document.createElement('div'),
        data () {
            return {
                text: text,
                show: true
            }
        }
    })
    console.log('toastDOM', toastDOM)
    document.body.appendChild(toastDOM.$el)

    setTimeout(() => {
        toastDOM.show = false
    }, duration)
}

function toastRegistry () {
    Vue.prototype.$toast = showToast
}

export default toastRegistry
